aboutsummaryrefslogtreecommitdiffstats
path: root/src/routes/(app)/[username]/request/[id]/+page.server.js
blob: 7beabfe6ef0806d8081b0087a8fe50d90ad1a4d7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import { auth, get } from '$lib/utils';
import { redirect } from '@sveltejs/kit';

export let load = async ({ cookies, depends, locals: { user }, params }) => {
	depends('app:invoice');
	if (!user) throw redirect(307, '/login');

	let { request } = await get(`/request/${params.id}`, auth(cookies));
	let { invoice } = request;

	if (invoice) {
		let { amount, received, uuid } = invoice;
		if (
			request.recipient.username === user.username &&
			((received && !amount) || received >= amount)
		)
			throw redirect(307, `/invoice/${uuid}/paid`);

		if (request.requester.username === user.username) throw redirect(307, `/send/${uuid}`);
	}

	return { request };
};